/*
 * Cartographer.java
 * 
 * Created by demory on Mar 28, 2009, 12:10:26 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.apps.cart;

import org.fpdev.apps.cart.modules.NetworkOps;
import org.fpdev.apps.cart.modules.FileOps;
import java.util.HashMap;
import java.util.Map;
import org.fpdev.apps.Application;
import org.fpdev.apps.cart.gui.CartGUI;
import org.fpdev.apps.cart.modules.EditorOps;
import org.fpdev.apps.cart.modules.StyleOps;
import org.fpdev.apps.cart.modules.SystemOps;

/**
 *
 * @author demory
 */
public class Cartographer extends Application implements CartEventProcessor {

  // the Cartographer graphical interface:
  private CartGUI gui_;
  
  // the main transit network:
  //private CartNetwork network_;

  private Map<Integer, Editor> editors_;
  //private Editor activeEditor_;
  private EditorFactory editorFactory_;

  private Map<CartModule.ID, CartModule> modules_;
  // the specialized operations classes:
  //private CartNetworkOps networkOps_;
  //private CartFileOps fileOps_;

  public Cartographer() {
    super("cartographer", false);

    editors_ = new HashMap<Integer, Editor>();
    editorFactory_ = new EditorFactory(this);

    initModules();
    
    gui_ = new CartGUI(this);

  }

  private void initModules() {
    modules_ = new HashMap<CartModule.ID, CartModule>();

    modules_.put(CartModule.ID.SYSTEM, new SystemOps(this));
    modules_.put(CartModule.ID.EDITOR, new EditorOps(this));
    modules_.put(CartModule.ID.FILE, new FileOps(this));
    modules_.put(CartModule.ID.NETWORK, new NetworkOps(this));
    modules_.put(CartModule.ID.STYLE, new StyleOps(this));

  }
  
  public CartGUI getGUI() {
    return gui_;
  }

  public CartModule getModule(CartModule.ID id) {
    return modules_.get(id);
  }

  public Editor getEditor(int eid) {
    return editors_.get(eid);
  }

  public Editor getActiveEditor() {
    if(gui_.getActiveEditorFrame() == null) return null;
    return gui_.getActiveEditorFrame().getEditor();
  }

  public EditorFactory getEditorFactory() {
    return editorFactory_;
  }

  public void addEditor(Editor ed) {
    editors_.put(ed.getID(), ed);
    gui_.addEditor(ed);
  }

  public CartEventResult handleEvent(CartEvent event) {
    
    Editor ed = event.propertyDefined("eid") ?
      editors_.get(event.getIntProperty("eid")) :
      getActiveEditor();
    event.setEditor(ed);

    CartEventResult result = new CartEventResult();
    System.out.println("Main event handler: "+event.getType());
    for(CartModule.ID moduleID : event.getType().getModuleIDs()) {
      CartEventProcessor cep = modules_.get(moduleID);
      if(cep == null) {
        System.out.println("Undefined module: "+moduleID);
        continue;
      }
      CartEventResult subResult = cep.handleEvent(event);
      if(subResult.getAction() != null) {
        CartAction action = subResult.getAction(); //networkOps_.createActionFromEvent(ed, event);
        //if(action == null) return;
        if(action.doAction(this)) {
          ed.getHistory().addAction(action);
          gui_.updateUndoRedo(ed);
          ed.getFrame().getCanvas().repaint();
        }
      }
      result.addSubResult(subResult);
    }

    return result;
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    new Cartographer();
  }

}
